﻿using System.Data;
using ContractManagementSystem.DataAccessLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.BusinessLogicLayer
{
    public static class ClientProfileLogic
    {
        #region"Get client"
        /// <summary>
        /// Get all clients in table tblClients
        /// </summary>
        /// <returns></returns>
        public static DataTable SelectAllClient()
        {
            DataTable result = ClientDao.SelectAllClient();
            return result;
        }
        /// <summary>
        /// Get Client by ClientId
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public static DataTable SelectClientByClientId(string clientId)
        {
            DataTable result = ClientDao.SelectClientByClientId(clientId);
            return result;
        }
        /// <summary>
        /// Get client by user name of client
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static DataTable SelectClientByUserName(string user)
        {
            DataTable result = ClientDao.SelectClientByUserName(user);
            return result;
        }

        #endregion

        #region"Search"
        /// <summary>
        /// Search client in list by full name or company 
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="companyName"></param>
        /// <returns></returns>
        public static DataTable SearchClient(string fullName, string companyName)
        {
            DataTable result = ClientDao.SearchClient(fullName, companyName);
            return result;
        }
        #endregion

        #region"Insert/delete/update"
        /// <summary>
        /// Insert a new client
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static string InsertClient(ClientEntity client)
        {
            if (!CheckBeforeSave(client))
            {
                return null;
            }
            if (CheckExistEmailValue(client.Email))
            {
                return "Existing email!";
            }
            string insert = ClientDao.InsertClient(client);
            return insert;
        }

        /// <summary>
        /// update a client
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static string UpdateClient(ClientEntity client)
        {
            if (!CheckBeforeSave(client))
            {
                return null;
            }
            string update = ClientDao.UpdateClient(client);
            return update;
        }
        /// <summary>
        /// Delete a client
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static string DeleteClient(ClientEntity client)
        {
            string delete = ClientDao.DeleteClient(client);
            return delete;
        }
        #endregion

        //Check exists email
        public static bool CheckExistEmailValue(string value)
        {
            bool result = ClientDao.CheckExistEmailValue(value);
            return result;
        }

        //Check before saving
        public static bool CheckBeforeSave(ClientEntity client)
        {
            if(string.IsNullOrEmpty(client.FullName))
            {
                return false;
            }
            if(string.IsNullOrEmpty(client.CompanyName))
            {
                return false;
            }
            if(string.IsNullOrEmpty(client.PhoneNumber))
            {
                return false;
            }
            if(string.IsNullOrEmpty(client.Address))
            {
                return false;
            }
            if(string.IsNullOrEmpty(client.Email))
            {
                return false;
            }
            return true;
        }
    }
}
